home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
wdj0697.zip
/
SDKANN.ZIP
/
SOURCE.ZIP
/
MSTATER.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-01-09
|
42KB
|
1,384 lines
#include <assert.h>
#include "anntater.h"
#include <windowsx.h>
#include <commdlg.h>
#include <stdlib.h>
#include <string.h>
extern void InitCheckList(HINSTANCE);
#define OutputDebugString(s) (void)(s)
HWND MainDlgWin;
int MakeActive(HWND Window);
void MessagePump(int Time = 100);
int InCopy = FALSE;
int StopLoop = FALSE;
UINT WM_MSIN_RETURN = RegisterWindowMessage("MSIN_RETURN");
HWND MsMainWin;
HWND MsTopicView;
size_t GetEnvVar(const char* Name, char* Buffer, size_t Size)
{
#if defined(__WIN32__) || defined(__WIN32)
return GetEnvironmentVariable(Name, Buffer, Size);
#else
const char* Env = GetDOSEnvironment();
while(Env && *Env)
{
const char* Found = strstr(Env, Name);
if(Found == Env && Found[strlen(Name)] == '=')
{
strcpy(Buffer, Found+strlen(Name)+1);
return strlen(Name);
}
}
return 0;
#endif
}
HWND GetRealParent(HWND Target)
{
#if defined(__WIN32__) || defined(__WIN32)
return (HWND)GetWindowLong(Target, GWL_HWNDPARENT);
#else
return (HWND)GetWindowWord(Target, GWW_HWNDPARENT);
#endif
}
HWND OpenMsAnno()
{
HWND Anno = GetDlgItem(MsTopicView, 2);
if(!Anno)
PostMessage(MsMainWin, WM_COMMAND, 35376, 0);
MessagePump(500);
Anno = GetDlgItem(MsTopicView,2);
return Anno;
}
/* Attributes
* - window must be topmost
* - window must be dialog
* - window must contain child window with specific ID
* - window must be contain certain text
* - window must belong to certain app
*/
HWND FindTopOwnedWindow(HWND Owner, const char* Prefix, int Exact=FALSE)
{
OutputDebugString("FindTopOwnedWindow ");
OutputDebugString(Prefix);
OutputDebugString(" \n");
assert(IsWindow(Owner));
HWND Rover = GetDesktopWindow();
Rover = GetWindow(Rover, GW_CHILD);
while(Rover)
{
HWND Parent = (HWND)GetRealParent(Rover);
if(Parent == Owner)
{
char Title[256];
GetWindowText(Rover, Title, 255);
if(!Exact && !strncmp(Prefix, Title, strlen(Prefix)))
return Rover;
else if(!strcmp(Prefix, Title))
return Rover;
}
Rover = GetWindow(Rover, GW_HWNDNEXT);
}
return 0;
}
int MB(const char* Msg)
{
return MessageBox(MainDlgWin, Msg, "MsTater", MB_OK);
}
void MessagePump2(HWND Dialog, int Time = 100)
{
int StopLoop = FALSE;
SetTimer(Dialog, 45, Time, 0);
EnableWindow(Dialog, FALSE);
MSG Msg;
while(!StopLoop)
while(PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE))
{
switch(Msg.message)
{
case WM_KEYDOWN :
case WM_LBUTTONDOWN :
case WM_RBUTTONDOWN :
case WM_MBUTTONDOWN :
case WM_TIMER :
StopLoop = TRUE;
}
GetMessage(&Msg, NULL, 0, 0);
DispatchMessage(&Msg);
}
KillTimer(Dialog, 45);
EnableWindow(Dialog, TRUE);
}
#if 1
void MessagePump(int Time)
{
DWORD StartTime = GetTickCount();
MSG Msg;
while((GetTickCount() - StartTime) < Time)
while(PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE))
{
switch(Msg.message)
{
case WM_KEYDOWN :
case WM_LBUTTONDOWN :
case WM_RBUTTONDOWN :
case WM_MBUTTONDOWN :
StopLoop = TRUE;
}
GetMessage(&Msg, NULL, 0, 0);
DispatchMessage(&Msg);
}
}
#endif
int PushButton(HWND Dialog, int ButtonId, int Post = FALSE)
{
int Result = FALSE;
HWND Button = GetDlgItem(Dialog, ButtonId);
if(Button)
{
if(Post)
{
PostMessage(Dialog, WM_COMMAND, ButtonId,
MAKELONG(Button, BN_CLICKED));
MessagePump();
}
else
SendMessage(Dialog, WM_COMMAND, ButtonId,
MAKELONG(Button, BN_CLICKED));
Result = TRUE;
}
return Result;
}
#if 0
int CheckRadio(HWND Dialog, int FirstButton, int LastButton)
{
int Result = FALSE;
if(Button)
{
else
SendMessage(Dialog, WM_COMMAND, ButtonId,
MAKELONG(Button, BM_SETCHECK));
Result = TRUE;
}
return Result;
}
#endif
// OpenMsSearch - open the search dialog in Developer Studio
// we also make sure the full-text search property sheet is active
HWND OpenMsSearch(HWND Dialog, int Complain = TRUE, int And=FALSE)
{
char Buffer[256];
HWND Search = GetWindow(GetDesktopWindow(), GW_CHILD);
while(Search)
{
GetWindowText(Search, Buffer, sizeof(Buffer)-1);
if(!strcmp(Buffer, "Search") && MsMainWin == GetWindow(Search, GW_OWNER))
break;
Search = GetWindow(Search, GW_HWNDNEXT);
}
if(!Search)
{
if(Complain)
MessageBox(NULL, "Unable to open Search dialog [no child of main window]", "MsTater", MB_OK);
return 0;
}
assert(IsWindow(Search));
// MakeActive(Search);
// BringWindowToTop(Search);
GetWindowText(Search, Buffer, sizeof(Buffer)-1);
if(strcmp(Buffer, "Search"))
{
MessageBox(NULL, "Unable to open Search dialog [child title not 'Search']", Buffer, MB_OK);
return 0;
}
MessagePump2(Dialog);
HWND Prop = GetWindow(Search, GW_CHILD);
GetWindowText(Prop, Buffer, sizeof(Buffer)-1);
if(strcmp(Buffer, "Query"))
{
MessagePump2(Dialog, 1000);
Prop = GetWindow(Search, GW_CHILD);
GetWindowText(Prop, Buffer, sizeof(Buffer)-1);
if(strcmp(Buffer, "Query"))
MessageBox(NULL, "Please click on the 'Query' tab in the Search dialog and then press my OK button", "MsTater", MB_OK);
Prop = GetWindow(Search, GW_CHILD);
}
GetWindowText(Prop, Buffer, sizeof(Buffer)-1);
if(strcmp(Buffer, "Query"))
{
MessageBox(NULL, "Could not locate Query tab in Search Dialog", "MsTater", MB_OK);
return FALSE;
}
// push buttons the way we like...
if(And)
{
// subset == results of last query
CheckRadioButton(Prop, 0x01FF, 0x0202, 0x0201);
// Title and Contents
CheckRadioButton(Prop, 0x0209, 0x020A, 0x0209);
}
else
{
// Entire Contents
CheckRadioButton(Prop, 0x01FF, 0x0202, 0x01FF);
// Title only
CheckRadioButton(Prop, 0x0209, 0x020A, 0x020A);
}
MessagePump();
return Prop;
}
BOOL EnableControl(HWND Dialog, int Id, int On)
{
HWND Child = GetDlgItem(Dialog, Id);
if(Child != 0)
return EnableWindow(Child, On);
else
return FALSE;
}
typedef BOOL (WINAPI*SETFUNC)(HWND Window);
int MakeActive(HWND Window)
{
#if defined(__WIN32__) || defined(__WIN32)
SetForegroundWindow(Window)
#else
#define USER() GetModuleHandle("USER")
static int Loaded = FALSE;
static SETFUNC SetForegroundWindow;
assert(IsWindow(Window));
if(!Loaded)
{
SetForegroundWindow = (SETFUNC)GetProcAddress(USER(),"SetForegroundWindow");
Loaded = TRUE;
}
if(SetForegroundWindow)
SetForegroundWindow(Window);
else
SetActiveWindow(Window);
#endif
MessagePump(250);
return TRUE;
}
char SelectedFile[512];
// name of main dialog box
#define MAIN_DLG "MainDialog"
HINSTANCE CurrentInstance; // often need global for this
/* GetHelpDialog() - locate a dialog belonging to the help window
*
* Sometimes we need to find one of WinHelp's dialog windows.
* This function locates a top-level window whose parent is the
* WinHelp window. You can also specify the Id of a child window,
* in case you need to distinguish between two WinHelp dialogs.
*/
HWND GetHelpDialog(int ChildId=0)
{
HWND Help = FindWindow("MS_WINHELP", NULL);
HWND Rover = GetDesktopWindow();
Rover = GetWindow(Rover, GW_CHILD);
while(Rover)
{
HWND Parent = GetRealParent(Rover);
HWND GrandParent = 0;
if(Parent)
GrandParent = GetRealParent(Parent);
if(Parent == Help || GrandParent == Help)
{
if(!ChildId || GetDlgItem(Rover, ChildId))
return Rover;
}
Rover = GetWindow(Rover, GW_HWNDNEXT);
}
return 0;
}
#include <io.h>
int WDJ_PathFind(const char *InPath, const char *File,
char *OutPath, int Maxlen)
{
/* sanity check */
if(InPath && *InPath)
{
// for each directory in the path
while(*InPath)
{
char Dir[256];
const char*Rover = strchr(InPath, ';');
int Len = Rover ? (Rover - InPath) : strlen(InPath);
memcpy(Dir, InPath, Len);
Dir[Len] = '\0';
strcat(Dir, "\\");
strcat(Dir, File);
if(access(Dir, 0) != -1)
{
if(OutPath && Maxlen)
{
strcpy(OutPath, Dir);
return TRUE;
}
}
InPath += Len+1;
}
}
return FALSE;
}
const char* GetPath()
{
static char Path[256+1];
if(GetEnvVar("PATH", Path, sizeof(Path)))
return Path;
else
return 0;
}
/* FindHelpFile - user selected new .hlp, try to locate it
*
* When the user picks a different .hlp to annotate, this
* function tries to locate that .hlp file. First, it tries
* the last known path for that .hlp file, which would be
* recorded in the .ini file. If that fails, it then searches
* the DOS environment path. If that fails, then all the other
* buttons are disabled except for the one that lets the user
* tell us where the .hlp file is.
*/
int FindHelpFile(HWND Dialog, const char*File)
{
int Result = TRUE;
char Found[512];
GetPrivateProfileString("AnnTater", File, "", Found, sizeof(Found), "wdj.ini");
// if not found in .ini file
if(Found[0] == '\0')
{
const char*Search = GetPath();
if(!WDJ_PathFind(Search, File, Found, sizeof(Found)))
Result = FALSE;
}
if(Result == TRUE)
{
EnableControl(Dialog, ID_COPY, TRUE);
EnableControl(Dialog, ID_SELECTALL, TRUE);
SetDlgItemText(Dialog, ID_HELP_PATH, Found);
strcpy(SelectedFile, Found);
}
else
{
EnableControl(Dialog, ID_COPY, FALSE);
EnableControl(Dialog, ID_SELECTALL, FALSE);
SetDlgItemText(Dialog, ID_HELP_PATH,
"<press Locate button to specify path for this file>");
}
return Result;
}
// load RCDATA resource, assumes string terminated by 2 NULLs
int GetResource(const char *Name, char *Buffer)
{
int Len;
char *Data;
HGLOBAL Handle;
HRSRC Pos = FindResource(CurrentInstance, Name, RT_RCDATA);
if(Pos == NULL)
return FALSE;
Handle = LoadResource(CurrentInstance, Pos);
Data = (char *)LockResource(Handle);
for(Len=0; Data[Len] || Data[Len+1]; ++Len)
;
memcpy(Buffer, Data, Len+1);
UnlockResource(Handle);
FreeResource(Handle);
return Len;
}
char* StrDup(const char* S)
{
size_t Len = strlen(S) + 1;
char* Result = new char[Len];
if(Result)
memcpy(Result, S, Len);
return Result;
}
int IsHelp32 = FALSE;
int InitHelpVersion()
{
size_t Len;
HINSTANCE Inst;
char Path[512];
HWND Help = FindWindow("MS_WINHELP", NULL);
Inst = GetWindowInstance(Help);
Path[0] = '\0';
GetModuleFileName(Inst, Path, sizeof(Path));
Len = strlen(Path);
/* 32-bit module name is "WINHLP32.EXE" */
if(Len >= 12 && Path[Len-6] == '3' && Path[Len-5] == '2')
IsHelp32 = TRUE;
return TRUE;
}
int WDJ_EmptyClipboard(void)
{
HWND ClipWin = GetDesktopWindow(); /* any window will do */
if(OpenClipboard(ClipWin))
{
EmptyClipboard();
CloseClipboard();
return TRUE;
}
return FALSE;
}
int WDJ_SetClipboardText(char *Buffer)
{
int Result = FALSE;
HWND ClipWin = GetDesktopWindow(); /* any window will do */
if(OpenClipboard(ClipWin))
{
size_t Len = strlen(Buffer) + 1;
HGLOBAL ClipData = GlobalAlloc(GHND, Len);
if(ClipData != 0)
{
char *P = (char*)GlobalLock(ClipData);
strcpy(P, Buffer);
GlobalUnlock(ClipData);
if(SetClipboardData(CF_TEXT, ClipData))
Result = TRUE;
}
CloseClipboard();
}
return Result;
}
int WDJ_GetClipboardText(char *Buffer, UINT MaxSize)
{
int Result = FALSE;
HWND ClipWin = GetDesktopWindow(); /* any window will do */
if(OpenClipboard(ClipWin))
{
HGLOBAL ClipData;
ClipData = (HGLOBAL)GetClipboardData(CF_TEXT);
if(ClipData != NULL)
{
char *P = (char*)GlobalLock(ClipData);
if(P != 0)
{
UINT Len = strlen(P) + 1;
if(MaxSize > 0)
{
if(MaxSize < Len)
Len = MaxSize;
if(Len > 0)
memcpy(Buffer, P, Len-1);
Buffer[Len] = '\0';
}
Result = TRUE;
}
GlobalUnlock(ClipData);
}
CloseClipboard();
}
return Result;
}
//*********************************************************
class TTopic
{
public:
TTopic() : RsrcId_(0) {}
~TTopic() {}
int Load(HINSTANCE Inst, int TopicNum);
int IsFor(const char*FileName);
int GetFile(char* Buffer, int Max)
{ return GetIt(0, Buffer, Max); }
int GetDisplayTitle(char* Buffer, int Max)
{ return GetIt(1, Buffer, Max); }
int GetKeyword(char* Buffer, int Max)
{ return GetIt(2, Buffer, Max); }
int GetTitle(char* Buffer, int Max)
{ return GetIt(3, Buffer, Max); }
int GetText(char* Buffer, int Max)
{ return GetIt(4, Buffer, Max); }
void Clear() { RsrcId_ = 0; TopicNum_ = 0; }
private:
int GetIt(int Pos, char*Buffer, int Max);
HRSRC RsrcId_;
int TopicNum_;
};
TTopic ThisTopic;
int TTopic::Load(HINSTANCE Inst, int TopicId)
{
TopicNum_ = TopicId;
RsrcId_ = FindResource(Inst,
MAKEINTRESOURCE(TopicId), RT_RCDATA);
return RsrcId_ != NULL;
}
int TTopic::IsFor(const char* /*FileName*/)
{
if(!RsrcId_)
return FALSE;
char File[128];
GetFile(File, sizeof(File));
return strcmpi("win31wh.hlp", File);
}
int TTopic::GetIt(int Pos, char* Out, int Max)
{
int Result = FALSE;
if(!RsrcId_)
return Result;
char* Buffer = new char[1024*8];
if(Buffer)
{
if(GetResource(MAKEINTRESOURCE(TopicNum_), Buffer))
{
const char*Rover = Buffer;
while(Pos--)
Rover = Rover + strlen(Rover)+1;
if(Out && Max > 0)
{
for(int i=0; i < Max && *Rover; ++i)
*Out++ = *Rover++;
*Out = '\0';
Result = TRUE;
}
}
delete[] Buffer;
}
return Result;
}
//*********************************************************
typedef BOOL (*DLGCMDHANDLER)(HWND Dialog, UINT Notify,
int ControlId, HWND Control);
BOOL DialogCommand(DLGCMDHANDLER Handler, HWND Dialog,
WPARAM Param1, LPARAM Param2)
{
UINT Notify;
UINT ControlId;
HWND Control;
#if defined(__WIN32__)
Notify = HIWORD(Param1);
ControlId = LOWORD(Param1);
Control = (HWND)Param2;
#else
Notify = HIWORD(Param2);
ControlId = Param1;
Control = (HWND)LOWORD(Param2);
#endif
return Handler(Dialog, Notify, (int)ControlId, Control);
}
// FindHelp - locate a standalone helpfile window!
HWND FindHelp(void)
{
return FindWindow("MS_WINHELP", NULL);
}
int LoadTitles(HWND Listbox, const char*File)
{
char *Buffer = (char *)malloc(1024*16);
char *Title = Buffer;
SendMessage(Listbox, WM_SETREDRAW, 0, 0);
SendMessage(Listbox, LB_RESETCONTENT, 0, 0);
TTopic Topic;
for(int i=1; Topic.Load(CurrentInstance, i); ++i)
{
if(Topic.IsFor(File))
{
Topic.GetDisplayTitle(Title, 1024);
int Index = SendMessage(Listbox, LB_ADDSTRING, 0, (LPARAM)Title);
if(Index != CB_ERR)
{
ListBox_SetItemData(Listbox, Index, i);
}
}
}
SendMessage(Listbox, WM_SETREDRAW, 1, 0);
free(Buffer);
return TRUE;
}
enum { CANCEL = 9, REPLACE = 10, APPEND = 11,
ASK = 12, SKIP = 13 } Always=ASK;
#ifdef __BORLANDC__
# pragma argsused
#endif
static BOOL OnClobberDialogCommand(HWND Dialog, UINT Notify,
int ControlId, HWND Control)
{
if(ControlId == IDCANCEL)
{
EndDialog(Dialog, Always = CANCEL);
return FALSE;
}
else if(ControlId == ID_SKIP)
{
EndDialog(Dialog, SKIP);
if(IsDlgButtonChecked(Dialog, ID_STOPASKING))
Always = CANCEL;
return FALSE;
}
else if(ControlId == ID_REPLACE)
{
EndDialog(Dialog, REPLACE);
if(IsDlgButtonChecked(Dialog, ID_STOPASKING))
Always = REPLACE;
}
else if(ControlId == ID_APPEND)
{
EndDialog(Dialog, APPEND);
if(IsDlgButtonChecked(Dialog, ID_STOPASKING))
Always = APPEND;
}
return FALSE;
}
BOOL CALLBACK __export ClobberDialog(HWND Dialog, UINT Message,
WPARAM Param1, LPARAM Param2)
{
if(Message == WM_INITDIALOG)
{
return TRUE;
}
else if(Message == WM_COMMAND)
{
return DialogCommand(OnClobberDialogCommand,
Dialog, Param1, Param2);
}
return FALSE; /* FALSE == we didn't process msg */
}
static HWND StartSearch(HWND Dialog, const char* Keyword, int And)
{
// search dialog might already be open, try it
HWND Search = OpenMsSearch(Dialog, FALSE, And);
if(!Search)
{
PostMessage(MsMainWin, WM_COMMAND, 35329, 0);
int i;
for(i=0; i < 10 && !Search; ++i)
{
MessagePump2(Dialog);
Search = OpenMsSearch(Dialog, i >= 9, And);
}
if(!Search)
return 0;
}
assert(IsWindow(Search));
// now we have the search dialog open to the Query tab.
// get the handle to the search text combobox
MessagePump();
HWND SearchCombo = GetDlgItem(Search, 0x01F5);
if(!SearchCombo)
{
MB("Could not locate search string combobox in search dialog!");
return 0;
}
assert(IsWindow(SearchCombo));
SendMessage(SearchCombo, CB_INSERTSTRING, 0, (LPARAM)Keyword);
SendMessage(SearchCombo, CB_SETCURSEL, 0, 0);
// push Query button
assert(IsWindow(GetDlgItem(Search, 0x01FA)));
OutputDebugString("about to push the search button\n");
PushButton(Search, 0x01FA, TRUE);
OutputDebugString("pushed the search button\n");
return Search;
}
// GetQueryResults - return handle to window containing query results
static HWND GetQueryResults(HWND Dialog, char* Buffer, const char* Title)
{
// now have to wait for results of query
// 1) wait a second
MessagePump2(Dialog, 1000);
// while the "delay" dialog is up, just loop
int i;
for(i=0; FindTopOwnedWindow(MsMainWin, "Query", TRUE) && i < 30; ++i)
MessagePump(500);
// now search for query results window
HWND ResultWin = FindTopOwnedWindow(MsMainWin, "Query Results");
for(i = 0; i < 40 && !ResultWin; ++i)
{
ResultWin = FindTopOwnedWindow(MsMainWin, "Query Results");
if(!ResultWin &&
(ResultWin=FindWindow("#32770", "Microsoft Developer Studio")) != 0)
{
PushButton(ResultWin, IDCANCEL);
MessagePump();
wsprintf(Buffer, "Could not locate '%s', skipping to next topic.",
Title);
MB(Buffer);
return 0;
}
MessagePump();
}
if(!ResultWin)
{
MB("Could not locate query results dialog box!");
return 0;
}
return ResultWin;
}
// ShowTopic - display a given topic in the target help file
static int ShowTopic(HWND Dialog, int LbIndex)
{
char Buffer[256];
assert(IsWindow(Dialog));
// MakeActive(MsMainWin);
OutputDebugString("Enter ShowTopic()\n");
SetWindowText(Dialog, "enter ShowTopic()");
// MakeActive(Dialog);
int Topic = 0x7FFF & (int)ListBox_GetItemData(
GetDlgItem(Dialog, ID_TOPICTITLES), LbIndex);
ThisTopic.Load(CurrentInstance, Topic);
char Keyword[128], AndKeyword[128];
ThisTopic.GetKeyword(Keyword, sizeof(Keyword));
strlwr(Keyword);
char* And = strstr(Keyword, " and ");
if(And)
{
*And = '\0';
strcpy(AndKeyword, And+5);
}
else
strcpy(AndKeyword, "");
char Title[128];
ThisTopic.GetTitle(Title, sizeof(Title));
HWND Search = StartSearch(Dialog, Keyword, FALSE);
if(!Search)
return -1;
HWND ResultWin = GetQueryResults(Dialog, Buffer, Title);
if(!ResultWin)
return 0;
assert(IsWindow(ResultWin));
GetWindowText(ResultWin, Buffer, sizeof(Buffer)-1);
char *S = strchr(Buffer, '(');
int NumTopics = 2;
if(S)
NumTopics = atoi(S+1);
assert(NumTopics >= 1);
if(NumTopics > 1)
{
OutputDebugString("NumTopics > 1 \n");
BringWindowToTop(ResultWin);
// if we have additional keywords to narrow the search
if(AndKeyword[0])
{
OutputDebugString(" Doing additional search with:");
OutputDebugString("\n");
// dismiss results window
SendMessage(ResultWin, WM_MSIN_RETURN, 0, 0);
Search = StartSearch(Dialog, AndKeyword, TRUE);
if(!Search)
return -1;
ResultWin = GetQueryResults(Dialog, Buffer, Title);
if(!ResultWin)
return 0;
assert(IsWindow(ResultWin));
GetWindowText(ResultWin, Buffer, sizeof(Buffer)-1);
char *S = strchr(Buffer, '(');
int NumTopics = 2;
if(S)
NumTopics = atoi(S+1);
assert(NumTopics >= 1);
if(NumTopics == 1)
{
// Whew! additional keywords did narrow the search
// send secret message to go to topic
SendMessage(ResultWin, WM_MSIN_RETURN, 0, 0);
return TRUE;
}
}
BringWindowToTop(Dialog);
MB("Please select the correct topic to annotate, then press Enter.");
// now see if result window is still there
ResultWin = FindTopOwnedWindow(MsMainWin, "Query Results");
if(ResultWin)
SendMessage(ResultWin, WM_MSIN_RETURN, 0, 0);
}
else
{
// cool, secret message to dismiss dialog
SendMessage(ResultWin, WM_MSIN_RETURN, 0, 0);
}
// Finally! We think we have the topic displayed
return TRUE;
}
void DisplayItem(HWND Dialog, int Index)
{
HWND Listbox = GetDlgItem(Dialog, ID_TOPICTITLES);
int TopicNum = 0x7FFF & SendDlgItemMessage(Dialog,
ID_TOPICTITLES, LB_GETITEMDATA, Index, 0);
ThisTopic.Load(CurrentInstance, TopicNum);
/* set edit control text */
SendDlgItemMessage(Dialog, ID_TOPICTEXT,
EM_SETSEL, 1, MAKELPARAM(0,-1));
char*Buffer = new char[8*1024];
if(Buffer)
{
Buffer[0] = '\0';
ThisTopic.GetText(Buffer, 8*1024);
SendDlgItemMessage(Dialog, ID_TOPICTEXT, EM_REPLACESEL,
0, (LPARAM)Buffer);
ThisTopic.GetTitle(Buffer, 8*1024);
SetDlgItemText(Dialog, ID_TOPIC_TITLE, Buffer);
ThisTopic.GetKeyword(Buffer, 8*1024);
SetDlgItemText(Dialog, ID_TOPIC_KEYWORD, Buffer);
delete[] Buffer;
}
SendDlgItemMessage(Dialog, ID_TOPICTEXT, EM_SETSEL, 0, 0);
SendDlgItemMessage(Dialog, ID_TOPICTEXT, EM_LINESCROLL, 0,
MAKELPARAM(-255, 0));
}
void CheckItem(HWND Dialog, int Item)
{
HWND Listbox = GetDlgItem(Dialog, ID_TOPICTITLES);
// add a checkmark to this item
int Old = ListBox_GetItemData(Listbox, Item);
ListBox_SetItemData(Listbox, Item, Old|0x8000);
// make sure it is not selected
ListBox_SetSel(Listbox, FALSE, Item);
ListBox_SetTopIndex(Listbox, (Item < 4) ? 0 : Item - 4);
// schedule a redraw
InvalidateRect(Listbox, NULL, FALSE);
}
static BOOL CopySelections(HWND Dialog)
{
char *Buffer, *Text;
LRESULT Count;
int *Items, i;
// HWND Help = FindHelp();
HWND Listbox = GetDlgItem(Dialog, ID_TOPICTITLES);
StopLoop = FALSE;
Count = SendDlgItemMessage(Dialog, ID_TOPICTITLES,
LB_GETSELCOUNT, 0, 0);
if(Count == 0)
return FALSE;
Items = (int *)malloc(sizeof(int *)*(int)Count);
if(!Items)
{
MessageBox(Dialog, "Out of memory!", "AnnTater", MB_OK);
return FALSE;
}
SendDlgItemMessage(Dialog, ID_TOPICTITLES,
LB_GETSELITEMS, (WPARAM)Count, (LPARAM)Items);
Buffer = (char *)malloc(1024*16);
if(!Buffer)
{
MessageBox(Dialog, "Out of memory!", "AnnTater", MB_OK);
return FALSE;
}
Text = (char *)malloc(1024*16);
if(!Text)
{
free(Buffer);
MessageBox(Dialog, "Out of memory!", "AnnTater", MB_OK);
return FALSE;
}
int j;
for(i= 0; i < Count && Always != CANCEL; ++i)
{
if(StopLoop)
break;
DisplayItem(Dialog, Items[i]);
int Status = ShowTopic(Dialog, Items[i]);
if(Status == FALSE)
continue;
else if(Status == -1)
break;
HWND AnnDlg = OpenMsAnno();
assert(IsWindow(AnnDlg));
// read in our annotation text
ThisTopic.GetText(Text, 1024*16);
// now get existing annotation text from window
Buffer[0] = '\0';
SendMessage(AnnDlg, WM_GETTEXT, 1024*16, (LPARAM)Buffer);
/* if annotation already exists */
if(Buffer[0])
{
if(strstr(Buffer, Text))
{
// PushButton(AnnDlg, IDCANCEL, TRUE);
CheckItem(Dialog, Items[i]);
continue;
}
int Result = Always;
if(Always == ASK)
{
MakeActive(Dialog);
Result = DialogBox(CurrentInstance, "Clobber",
AnnDlg, (DLGPROC)ClobberDialog);
SetWindowText(Dialog, "back from helper dialog");
}
if(Result == CANCEL || Result == SKIP)
{
// PushButton(AnnDlg, IDCANCEL, TRUE);
continue;
}
else if(Result == APPEND)
;
else if(Result == REPLACE)
Buffer[0] = '\0';
}
strcat(Buffer, Text);
SendMessage(AnnDlg, EM_SETSEL, 0, MAKELPARAM(0, -1));
SendMessage(AnnDlg, EM_REPLACESEL, 0, (LPARAM)Buffer);
MessagePump();
CheckItem(Dialog, Items[i]);
}
free(Text);
free(Items);
free(Buffer);
StopLoop = FALSE;
return TRUE;
}
HWND WindowFromInstance(HINSTANCE Instance)
{
HWND Rover = GetWindow(GetDesktopWindow(), GW_CHILD);
while(Rover)
{
if(Instance == (HINSTANCE)GetWindowInstance(Rover))
return Rover;
else
Rover = GetWindow(Rover, GW_HWNDNEXT);
}
return (HWND)0;
}
static BOOL OnMainDialogCommand(HWND Dialog, UINT Notify,
int ControlId, HWND Control)
{
if(ControlId == IDOK || ControlId == IDCANCEL)
{
EndDialog(Dialog, ControlId == IDOK);
return FALSE;
}
else if(ControlId == ID_HELP && Notify == BN_CLICKED)
{
static char*Help =
"MsTater: The Windows Developer's Journal helpfile annotater.\r\n"
"Use at your own risk!\r\n\r\n"
"Select one or more "
"annotations (or press the Select All button). Then, "
"press Copy to install your selections in the InfoViewer "
"(then stand well back). You may want to back up your "
".ano file before starting this process. ";
MessageBox(Dialog, Help, "AnnTater", MB_OK);
}
else if(ControlId == ID_FIND_FILE && Notify == BN_CLICKED)
{
HWND Combo = GetDlgItem(Dialog, ID_FILES);
char File[256];
char Key[128];
int Index = ComboBox_GetCurSel(Combo);
strcpy(File,
(const char*)ComboBox_GetItemData(Combo, Index)
);
strcpy(Key, File);
OPENFILENAME DialogArgs;
memset(&DialogArgs, 0, sizeof(DialogArgs));
DialogArgs.lStructSize = sizeof(DialogArgs);
DialogArgs.hwndOwner = Dialog;
char Filter[256];
memset(Filter, 0, sizeof(Filter));
strcpy(Filter, "Selected help file");
strcpy(Filter+strlen(Filter)+1, File);
DialogArgs.lpstrFilter = Filter;
DialogArgs.nFilterIndex = 1;
char Title[256];
wsprintf(Title, "Locate selected .hlp file (%s)", File);
DialogArgs.lpstrTitle = Title;
DialogArgs.lpstrFile = File;
DialogArgs.nMaxFile = 256;
DialogArgs.Flags = OFN_FILEMUSTEXIST | OFN_SHOWHELP;
if(GetOpenFileName(&DialogArgs))
{
WritePrivateProfileString("AnnTater", Key, File, "wdj.ini");
strcpy(SelectedFile, File);
PostMessage(Dialog, WM_COMMAND, ID_FILES, MAKELONG(
GetDlgItem(Dialog, ID_FILES), CBN_SELCHANGE));
}
}
else if(ControlId == ID_FILES && Notify == CBN_SELCHANGE)
{
char File[256];
File[0] = '\0';
int Index = ComboBox_GetCurSel(Control);
if(Index != CB_ERR)
{
strcpy(File,
(const char*)ComboBox_GetItemData(Control, Index)
);
// new .hlp file selected, attempt to set path
if(!FindHelpFile(Dialog, File))
return TRUE;
LoadTitles(GetDlgItem(Dialog, ID_TOPICTITLES), File);
SetDlgItemText(Dialog, ID_TOPIC_KEYWORD, "");
SetDlgItemText(Dialog, ID_TOPIC_TITLE, "");
SetDlgItemText(Dialog, ID_TOPICTEXT, "");
WinHelp(Dialog, SelectedFile, HELP_QUIT, 0);
MessagePump();
WinHelp(Dialog, SelectedFile, HELP_CONTENTS, 0);
InitHelpVersion();
MessagePump();
MoveWindow(FindWindow("MS_WINHELP", NULL),
0, 0,
100 + GetSystemMetrics(SM_CXSCREEN)/3,
100 + GetSystemMetrics(SM_CYSCREEN)/3, TRUE);
MakeActive(Dialog);
}
}
else if(ControlId == ID_COPY)
{
Always = ASK;
if(InCopy)
{
StopLoop = TRUE;
return TRUE;
}
InCopy = TRUE;
SetWindowText(Control, "Stop copy!");
CopySelections(Dialog);
SetWindowText(Control, "Copy");
InCopy = FALSE;
SetWindowText(Dialog, "Windows Developer's Journal Annotater");
MakeActive(Dialog);
return FALSE;
}
else if(ControlId == ID_SELECTALL)
{
int Count = SendDlgItemMessage(Dialog, ID_TOPICTITLES,
LB_GETCOUNT, 0, 0);
SendDlgItemMessage(Dialog, ID_TOPICTITLES, LB_SELITEMRANGE, 1,
MAKELPARAM(0, Count-1));
int Index = SendDlgItemMessage(Dialog, ID_TOPICTITLES,
LB_GETSELCOUNT, 0, 0);
EnableControl(Dialog, ID_COPY, Index != 0);
return FALSE;
}
else if(ControlId == ID_TOPICTITLES)
{
OutputDebugString(">>>>>>>>>>>ID_TOPICTITLES");
/* if selection changes, update edit box */
if(Notify == LBN_SELCHANGE)
{
OutputDebugString(" >>>>>>>LBN_SELCHANGE");
LRESULT Index;
/* enable/disable buttons, if we have selections or not */
Index = SendDlgItemMessage(Dialog, ID_TOPICTITLES,
LB_GETSELCOUNT, 0, 0);
EnableControl(Dialog, ID_COPY, Index != 0);
if(Index != 0)
{
/* load edit control with text from sel with caret */
Index = SendDlgItemMessage(Dialog, ID_TOPICTITLES,
LB_GETCARETINDEX, 0, 0L);
// ShowTopic(Dialog, Index);
SetWindowText(Dialog, "Windows Developer's Journal Annotater");
int TopicNum = 0x7FFF & SendDlgItemMessage(Dialog,
ID_TOPICTITLES, LB_GETITEMDATA, Index, 0);
ThisTopic.Load(CurrentInstance, TopicNum);
// MakeActive(Dialog);
}
else
ThisTopic.Clear();
/* set edit control text */
SendDlgItemMessage(Dialog, ID_TOPICTEXT,
EM_SETSEL, 1, MAKELPARAM(0,-1));
char*Buffer = new char[8*1024];
if(Buffer)
{
Buffer[0] = '\0';
ThisTopic.GetText(Buffer, 8*1024);
SendDlgItemMessage(Dialog, ID_TOPICTEXT, EM_REPLACESEL,
0, (LPARAM)Buffer);
ThisTopic.GetTitle(Buffer, 8*1024);
SetDlgItemText(Dialog, ID_TOPIC_TITLE, Buffer);
ThisTopic.GetKeyword(Buffer, 8*1024);
SetDlgItemText(Dialog, ID_TOPIC_KEYWORD, Buffer);
delete[] Buffer;
}
SendDlgItemMessage(Dialog, ID_TOPICTEXT, EM_SETSEL, 0, 0);
}
OutputDebugString("<<<<<<<<<<<<<<<ID_TOPICTITLES\n");
return FALSE;
}
return FALSE; /* FALSE == we didn't process the message */
}
/* InitFileList - initialize combo-box of files.
*
* Here we make a pass over all the annotations and
* form the list of the names of the files that are
* referred to by these annotations. The filenames
* are stored in a combobox.
*/
void InitFileList(HWND Dialog, int ComboId)
{
HWND Combo = GetDlgItem(Dialog, ComboId);
int Index;
Index = ComboBox_AddString(Combo, "MFC (mfc30.hlp)");
ComboBox_SetItemData(Combo, Index, "mfc30.hlp");
Index = ComboBox_AddString(Combo, "Win32 API (win32.hlp, api32.hlp)");
ComboBox_SetItemData(Combo, Index, "win32.hlp");
Index = ComboBox_AddString(Combo, "Windows 3.1 API (win31wh.hlp)");
ComboBox_SetItemData(Combo, Index, "win31wh.hlp");
}
BOOL CALLBACK __export MainDialog(HWND Dialog, UINT Message,
WPARAM Param1, LPARAM Param2)
{
if(Message == WM_INITDIALOG)
{
MainDlgWin = Dialog;
LoadTitles(GetDlgItem(Dialog, ID_TOPICTITLES), 0);
SetDlgItemText(Dialog, ID_TOPIC_KEYWORD, "");
SetDlgItemText(Dialog, ID_TOPIC_TITLE, "");
SetDlgItemText(Dialog, ID_TOPICTEXT, "");
return TRUE;
}
#if 0
else if(Message == WM_TIMER)
{
MakeActive(Dialog);
KillTimer(Dialog, 1);
BringWindowToTop(Dialog);
SetActiveWindow(Dialog);
SetFocus(Dialog);
}
#endif
else if(Message == WM_USER + 999)
{
MakeActive(Dialog);
}
else if(Message == WM_COMMAND)
{
return DialogCommand(OnMainDialogCommand,
Dialog, Param1, Param2);
}
else if(Message == WM_DESTROY && SelectedFile[0])
WinHelp(Dialog, SelectedFile, HELP_QUIT, 0);
return FALSE; /* FALSE == we didn't process msg */
}
// FindMsWindows - locate handles to all the windows we care about
int FindMsWindows()
{
char Buffer[1024];
HWND Desk = GetDesktopWindow();
HWND Rover = GetWindow(Desk, GW_CHILD);
while(Rover)
{
GetWindowText(Rover, Buffer, sizeof(Buffer)-1);
if(!strncmpi(Buffer, "Microsoft Developer Studio", 26))
break;
Rover = GetWindow(Rover, GW_HWNDNEXT);
}
if(Rover == 0)
{
MessageBox(NULL, "Start Microsoft Developer Studio before running MsTater.", "MsTater", MB_OK);
return FALSE;
}
if(!strstr(Buffer, "InfoViewer"))
{
MessageBox(NULL, "Open (and maximize) the InfoViewer window in Developer Studio before running MsTater.", "MsTater", MB_OK);
return FALSE;
}
MsMainWin = Rover;
// Now we have to descend down a long tree to find the annotation edit window
Rover = GetWindow(MsMainWin, GW_CHILD);
while(Rover)
{
GetClassName(Rover, Buffer, sizeof(Buffer)-1);
if(!strcmpi(Buffer, "MDIClient"))
break;
Rover = GetWindow(Rover, GW_HWNDNEXT);
}
if(Rover == 0)
{
MessageBox(NULL, "MsTater could not talk to Developer Studio. [missing MDIClient]", "MsTater", MB_OK);
return FALSE;
}
Rover = GetWindow(Rover, GW_CHILD);
while(Rover)
{
GetWindowText(Rover, Buffer, sizeof(Buffer)-1);
if(!strcmpi(Buffer, "InfoViewer Topic"))
break;
Rover = GetWindow(Rover, GW_HWNDNEXT);
}
if(Rover == 0)
{
MessageBox(NULL, "MsTater could not talk to Developer Studio. [missing InfoViewer Topic window]", "MsTater", MB_OK);
return FALSE;
}
Rover = GetWindow(Rover, GW_CHILD);
Rover = GetWindow(Rover, GW_CHILD);
while(Rover)
{
GetWindowText(Rover, Buffer, sizeof(Buffer)-1);
if(!strcmpi(Buffer, "TopicParent"))
break;
Rover = GetWindow(Rover, GW_HWNDNEXT);
}
if(Rover == 0)
{
MessageBox(NULL, "MsTater could not talk to Developer Studio. [missing TopicParent window]", "MsTater", MB_OK);
return FALSE;
}
Rover = GetWindow(Rover, GW_CHILD);
while(Rover)
{
GetWindowText(Rover, Buffer, sizeof(Buffer)-1);
if(!strcmpi(Buffer, "TopicView"))
break;
Rover = GetWindow(Rover, GW_HWNDNEXT);
}
if(Rover == 0)
{
MessageBox(NULL, "MsTater could not talk to Developer Studio. [missing TopicView window]", "MsTater", MB_OK);
return FALSE;
}
MsTopicView = Rover;
if(!OpenMsAnno())
{
MessageBox(NULL, "MsTater could not talk to Developer Studio. [Could not open annotation window]", "MsTater", MB_OK);
return FALSE;
}
return TRUE;
}
#ifdef __BORLANDC__
#pragma argsused
#endif
int WINAPI WinMain(HINSTANCE Instance, HINSTANCE Previous,
LPSTR CommandLine, int ShowCommand)
{
CurrentInstance = Instance;
InitCheckList(Instance);
if(FindMsWindows())
{
DialogBox(Instance, MAIN_DLG, NULL, (DLGPROC)MainDialog);
}
return 0;
}